//index.js
//获取应用实例
const app = getApp();
var url = getApp().globalData.ApiUrl;
var userInfo = wx.getStorageSync('userInfo');
var timeformat = require('../../utils/timeformat.js');
Page({
  data: {
    userInfo: {},
    nickname: userInfo.nickName,
    imgUrl: [],
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    hr: '', //小时
    min: '', //分钟
    sec: '', //秒
    indicatorDots: true,
    autoplay: true,
    interval: 3000,
    duration: 1000,  
    showView: false,
    num: 1,   //input默认为1
		detailData: '', //商品详情具体数据
		goods_storage: '',  //商品库存
		goods_price: '', //商品价格
		specALLArr: [],  //商品规格名称
		goods_list: [],
		propertyChildNames: '', //已选中的商品
		buyGoods_id: '', //购物车要传的商品id
    minusStatus: false,    //使用data数据设置样式名
    showLuckMoney: false,
    showModalStatus: false,
    banner_list: [],    // 顶部banner图列表
    limit_list: [],     // 限时特惠商品
    hot_limit: 0,
    hot_list: [],      // 获取热卖商品
    current: 0,
    current_limit: 0,
    person: "",   // 分享红包人id
    secretkey: "",  // 红包分享比对密钥
    list: [],   // 分享红包列表
    fen_id: [],
    fen_url: [],
    fen_img: '',
    star_time: 0,
    end_time: 0,
		shopType: "tobuy",//购物类型，加入购物车或立即购买，默认为加入购物车
    goods_img: '',  // 商品图片
    goods_image: '',
    goods_id: 0,
    showLimit: true,
    showSure: true
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    });
  },
  onLoad: function (options) {
    var key = wx.getStorageSync("key");
    if (!key) {
      wx.navigateTo({
        url: '/pages/authorize/index',
      });
    }
    // 是否具有领取新人红包的权限   
    wx.request({
      url: url + '/mobile/index.php?act=member_redpacket&op=newbie_redpacket_jurisdiction',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        key: key,
      },
      success: (res) => {
        if (res.data.code == 200) {
          var redpacket_jurisdiction = res.data.datas.redpacket_jurisdiction;
          if (redpacket_jurisdiction == false) {
            this.closeLuckMoney();
          } else {
            this.getNewCoupns();
          };
          this.setData({
            red_jurisd: res.data.datas.redpacket_jurisdiction,
          })
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.msg,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    }); 
    // 获取广告banner图
    wx.request({     // 小程序顶部
      url: url + '/mobile/index.php?act=api&op=get_banner',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        banner_id: app.globalData.index_banner
      },
      success: (res) => {
        if (res.data.code == 200) {
          var arr = [];
          var imgUrl = [];
          for (var i in res.data.datas) {
            arr.push(res.data.datas[i]);
            imgUrl.push(url + arr[i].adv_content)
          }
          console.log(arr);
          this.setData({
            banner_list: arr,
            imgUrl: imgUrl
          })
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    });  
    wx.request({    // 分享红包
      url: url + '/mobile/index.php?act=api&op=get_banner',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: {
        banner_id: app.globalData.index_share
      },
      success: (res) => {
        if (res.data.code == 200) {
          var arr = [];
          var fen_id = [];
          var fen_url = [];
          var fen_img = [];
          for (var i in res.data.datas) {
            arr.push(res.data.datas[i]);
            fen_id.push(arr[i].adv_url.replace(/[^0-9]/ig, ""));
            fen_url.push(arr[i].adv_url);
            fen_img.push(url + arr[i].adv_content);
            // if (arr[i].adv_id == 19) {
            var share_url = arr[i].adv_url
            // }
          }
          this.setData({
            fen_id: fen_id,
            fen_url: fen_url,
            fen_img: fen_img,
            share_url: share_url
          })
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // 倒计时
  count_down: function (allsec) {
    var that = this;
    clearTimeout(that.data.timer);   // 清楚定时针
    that.setData({
      day: timeformat.timeformat(allsec).days+'天',
      hr: timeformat.timeformat(allsec).hrs,
      min: timeformat.timeformat(allsec).mins,
      sec: timeformat.timeformat(allsec).second
    });
    if (allsec <= 0) {
      that.setData({
        day: '0天',
        hr: '00',
        min: '00',
        sec: '00'
      });
      // timeout则跳出递归
      return;
      
    };
    that.data.timer = setTimeout(function () {
      // 放在最后--
      allsec -= 1000;
      that.count_down(allsec);
    }, 1000);
  },

  searchFocus: function (){
    wx.navigateTo({
      url: '/pages/search/index'
    })
  },
  // 获取新人红包列表
  getNewCoupns: function (e) {
    wx.request({
      url: url + '/mobile/index.php?act=index&op=get_newbie_redpacket_lists',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'POST',
      success: (res) => {
        if (res.data.code == 200) {
          var list = res.data.datas;
          if (list.length == 0) {
            this.setData({
              showLuckMoney: false
            });
            return false
          };
          if (list.length != 0) {
            for (var i in list) {
              var rpacket_t_id = list[i].rpacket_t_id;
            }
            this.setData({
              showLuckMoney: true,
              rpacket_t_id: rpacket_t_id
            })
          }
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  toScan: function () {
    wx.switchTab({
      url: '/pages/trace/index',
    })
  },
  // 领取新人红包
  getLuckMoney: function(e) {
    var that = this;
    getApp().saveFormId(e.detail.formId);
    var key = wx.getStorageSync('key');
    wx.request({
      url: url + '/mobile/index.php?act=member_redpacket&op=get_newbie_redpacket',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: "POST",
      dataType: "json",
      data: {
        key: key,
        rpacket_t_id: that.data.rpacket_t_id
      },
      success: (res) => {
        if (!key) {          // 如果没有登录，挑战登录页面
          wx.navigateTo({
            url: '/pages/authrize/index',
          })
        }
        if (res.data.code == 200) {      // 红包领取成功
          wx.showToast({
            title: res.data.datas,
            duration: 2000,
            icon: "success"
          })
        }
        if (res.data.code == 400) {        // 已经领取过红包
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // 点击banner
  tapBanner: function(e){
    var adv_url = e.currentTarget.dataset.adv_url;
    var num = adv_url.replace(/[^0-9]/ig, "");
    wx.navigateTo({
      url: "/pages/new-special/index?adv_url=" + num,
    })
  },

  // 获取红包分享时携带的参数
  toGetConpouns: function (e){
    var redpack_t_id = e.currentTarget.dataset.redpack_t_id;
    this.setData({
      redpack_t_id: redpack_t_id
    })
    var key = wx.getStorageSync("key");
    getApp().saveFormId(e.detail.formId);
    wx.showLoading({
      title: '加载中,请稍后...',
      mask: true,
    });
    wx.request({
      url: url + '/mobile/index.php?act=member_redpacket&op=get_share_parameter',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      dataType: "json",
      data: {
        key: key,
        rpacket_t_id: redpack_t_id,
      },
      success: (res) => {
        if (!key) {
          wx.navigateTo({
            url: '/pages/authorize/index',
          })
        };
        if (res.data.code == 200) {
          this.setData({
            person: res.data.datas.redpacket_share_person,   // 分享红包人id
            secretkey: res.data.datas.redpacket_share_secretkey,  // 红包分享比对密钥
            showModalStatus: !this.data.showModalStatus,
            share_price: res.data.datas.rpacket_t_price
          });
          wx.hideLoading();
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
          this.setData({
            showModalStatus: false
          })
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    });
  },
  onShareAppMessage: function(ops){
    var key = wx.getStorageSync("key");
    var red_url = this.data.share_url + '&redpacket_share_person=' + this.data.person + '&redpacket_share_secretkey=' + this.data.secretkey;
    this.share = true;
    return {
      title: this.data.nickname + '@你,送你一张'+ this.data.share_price +'元优惠券',
      path: red_url,
      imageUrl: 'https://www.zhuisushangcheng.com/data/upload/applet/redImg.png',
      success: (res) => {
        wx.request({
          url: url + '/mobile/index.php?act=member_redpacket&op=share_redpacket_callback_share',
          header: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: "POST",
          dataType: "json",
          data: {
            key: key,
            rpacket_t_id: this.data.redpack_t_id,
            redpacket_share_person: this.data.person,
            redpacket_share_secretkey: this.data.secretkey
          },
          success: (res) => {
            if (res.data.code == 200) {
              wx.showToast({
                title: res.data.datas,
                duration: 2000,
              })
              wx.navigateTo({
                url: red_url,
              })
            };
            if (res.data.code == 400) {
              if (res.data.login == 0 && res.data.login) {
                wx.navigateTo({
                  url: '/pages/authorize/index',
                })
              } else {
                wx.showToast({
                  title: res.data.datas.error,
                  icon: 'none',
                  duration: 1500,
                  mask: true
                })
              };
              this.setData({
                showModalStatus: false
              })
            };
          },
          fail: () => {
            wx.showToast({
              title: '人太多啦',
              icon: 'none',
              duration: 2000
            })
          }
        });
      },
      fail: () => {
        wx.showToast({
          title: '取消分享',
          icon: 'none',
          mask: true
        });
      }
    }
   
  },
  onReady: function (){
    this.alertview = this.selectComponent("#alertview");
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (this.share) {
      this.share = false;
      this.setData({
        showModalStatus: false,
      });
    };
    var key = wx.getStorageSync("key");
    if (!key) {
      wx.navigateTo({
        url: '/pages/authorize/index',
      });
      return;
    }
    // 营销活动---限时特惠
    var data = {
      activity_type: "secakill",
    };
    if (key) {
      data.key = key
    }
    wx.request({
      url: url + '/mobile/index.php?act=marketing_activity&op=get_activity_goods',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'GET',
      data: data,
      success: (res) => {
        if (res.data.code == 200) {
          var arr = [];
          var pro_price = [];
          var pro = [];
          let list = res.data.goods_list;
          for (var i in list) {
            arr.push(list[i]);
            pro.push(parseFloat(list[i].goods_price - list[i].promotion_price).toFixed(2));
          }
          if (arr.length == 0) {
            this.setData({
              showLimit: false,
            });
          } else {
            var end_time = arr[0].end_time * 1000;
            var now = new Date();
            var total_time = end_time - now.getTime();
            this.setData({
              limit_list: list,
              pro_price: pro
            });
            this.count_down(total_time);
          }
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
          this.setData({
            showLimit: false,
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    })
    // 获取热卖商品
    wx.request({
      url: url + '/mobile/index.php?act=index&op=get_hot_goods',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      dataType: 'json',
      method: 'POST',
      data: {
        limit: this.data.hot_limit
      },
      success: (res) => {
        if (res.data.code == 200) {
          var hot_arr = [];
          for (var i in res.data.datas) {
            hot_arr.push(res.data.datas[i])
          }
          this.setData({
            hot_list: hot_arr,
            hot_limit: hot_arr.length
          })
        };
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
      },
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  onHide: function (){
    
  },
	errorTap: function () {
		this.setData({
			showView: false,
      propertyChildNames: '',
      num: 1,
		});
	},
  // 跳转商品详情页
  toGoodsDetails: function (e){
    var goods_id = e.currentTarget.dataset.goods_id;
    wx.navigateTo({
      url: '/pages/group-detail/index?goods_id='+goods_id,
    });
  },
  // 立即购买
	toCreateOrder: function (e){
		var that = this;
    var dataset = e.currentTarget.dataset;
		var goods_id = e.currentTarget.dataset.goods_id;
    var goods_img = e.currentTarget.dataset.goods_img;
    var mobile = wx.getStorageSync("mobile");
    if (!mobile) {
      wx.navigateTo({
        url: '/pages/phone/index',
      })
    };
    that.getDetails(goods_id);
		that.setData({
			shopType: 'addShopCar',
      goods_image: goods_img,
      dataset: dataset
		});
  },
	
  /**
	 * 加入购物车
	 */
	tobuy: function (e){
		var that = this;
    var dataset = e.currentTarget.dataset;
    var goods_id = e.currentTarget.dataset.goods_id;
    var goods_img = e.currentTarget.dataset.goods_img;
    var mobile = wx.getStorageSync("mobile");
    if (!mobile) {
      wx.navigateTo({
        url: '/pages/phone/index',
      })
    };
    that.getDetails(goods_id);
		that.setData({
			shopType: 'tobuy',
      goods_img: goods_img
		});
	},
	/**
	* 获取商品详情
	*/
	getDetails: function (dataset) {
		var that = this;
		wx.request({
			url: url + '/mobile/index.php?act=goods&op=goods_detail',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json',
			method: 'GET',
			data: {
				goods_id: dataset
			},
			success: (res) => {
				if (res.data.code == 200) {
					var datas = res.data.datas;
          console.log(datas);
					var specObj = res.data.datas.goods_info.spec_name;
					var tmp = {}; //商品规格
					var tmp2 = {}; //商品尺寸
					var specALLArr = []; // 商品规格和商品
          var goods_spec = [];
          var spec_id = [];
          for (var spec_d in res.data.datas.goods_info.goods_spec) {
            goods_spec.push(res.data.datas.goods_info.goods_spec[spec_d]);  // 商品规格
            spec_id.push(spec_d);
            var spec = goods_spec.toString().replace(/,/g, " ");
          };
					for (var i in specObj) {
						tmp.id = i;
						tmp.name = specObj[i];
						tmp.twoData = [];   
						for (var j in res.data.datas.goods_info.spec_value[i]) {
							tmp2.id = j;
							tmp2.name = res.data.datas.goods_info.spec_value[i][j];
							tmp2.propertyID = i;
              tmp2.active = (j in res.data.datas.goods_info.goods_spec) ? true : false;
							tmp.twoData.push(JSON.parse(JSON.stringify(tmp2)));
						};
						specALLArr.push(JSON.parse(JSON.stringify(tmp)));
					};
          if (spec) {
            this.setData({
              propertyChildNames: spec,
            })
          };
          if (datas.goods_info.promotion_type == 'xianshiall') {
            that.setData({
              specALLArr: specALLArr,
              detailData: datas,  //商品详情
              buyGoods_id: datas.goods_info.goods_id, //购买的商品价格
              goods_price: datas.goods_info.promotion_price, //商品价格
              goods_storage: datas.goods_info.goods_storage, //商品库存
              showView: true,
            })
          } else {
            that.setData({
              specALLArr: specALLArr,
              detailData: datas,  //商品详情
              buyGoods_id: datas.goods_info.goods_id, //购买的商品价格
              goods_price: datas.goods_info.goods_price, //商品价格
              goods_storage: datas.goods_info.goods_storage, //商品库存
              showView: true,
            });
          };
				};
        if (res.data.code == 400) {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
        }
			},
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
		});
	},
	/**
   * 切换规格选中状态
   * @author chelfinn
   * @date 2018-09-18
   */
	clickTap: function (e) {
		var that = this;
    var ids = [];   // 选中的id
    var valus = [];  // 选中商品的名字
    var specALLArr = that.data.specALLArr;
    var childs = that.data.specALLArr[e.currentTarget.dataset.propertyindex].twoData;
    // 取消该分类下的子栏目所有的选中状态
    for (var i = 0; i < childs.length; i++) {
      that.data.specALLArr[e.currentTarget.dataset.propertyindex].twoData[i].active = false;
    }
    // 设置当前选中状态
    that.data.specALLArr[e.currentTarget.dataset.propertyindex].twoData[e.currentTarget.dataset.propertychildindex].active = true;
    that.setData({
      specALLArr: specALLArr
    });
    // 循环数组 获取点中状态时的规格
    for (var id in specALLArr) {
      for (var i in specALLArr[id].twoData) {
        if (specALLArr[id].twoData[i].active == true) {
          ids.push(specALLArr[id].twoData[i].id);  // 获取的商品选中时的id
          valus.push(specALLArr[id].twoData[i].name);   // 获取商品点中时的规格
        }
      };
    };
    var spec_id = ids.sort().join("|");   // 升序
    var spec_value = valus.toString().replace(/,/g, " ");  // 选中的规格
    var spec_list = that.data.detailData.spec_list;
    if (spec_value) {
      this.setData({
        propertyChildNames: spec_value,
      })
    }
    if (spec_id in spec_list) {
      that.setData({
        buyGoods_id: spec_list[spec_id], //获取购买的商品id
      });
      // 获取商品详情
      wx.request({
        url: url + '/mobile/index.php?act=goods&op=goods_detail',
        header: {
          'Content-Type': 'application/x-www-form-urlencoded'
        },
        dataType: 'json',
        method: 'GET',
        data: {
          goods_id: that.data.buyGoods_id
        },
        success: (res) => {
          if (res.data.code == 200) {
            var datas = res.data.datas;
            var specObj = res.data.datas.goods_info.spec_name;
            var tmp = {}; //商品规格
            var tmp2 = {}; //商品尺寸
            var specALLArr = []; // 商品贵和和商品
            for (var i in specObj) {
              tmp.id = i;
              tmp.name = specObj[i];
              tmp.twoData = [];
              for (var j in res.data.datas.goods_info.spec_value[i]) {
                tmp2.id = j;
                tmp2.name = res.data.datas.goods_info.spec_value[i][j];
                tmp2.propertyID = i;
                tmp.twoData.push(JSON.parse(JSON.stringify(tmp2)));
              }
              specALLArr.push(JSON.parse(JSON.stringify(tmp)));
            };
            if (datas.goods_info.promotion_type == 'xianshiall') {
              that.setData({
                detailData: datas,  //商品详情
                buyGoods_id: datas.goods_info.goods_id, //购买的商品id
                goods_price: datas.goods_info.promotion_price, //商品价格
                goods_storage: datas.goods_info.goods_storage, //商品库存
                showView: true,
              })
            } else {
              that.setData({
                detailData: datas,  //商品详情
                buyGoods_id: datas.goods_info.goods_id, //购买的商品价格
                goods_price: datas.goods_info.goods_price, //商品价格
                goods_storage: datas.goods_info.goods_storage, //商品库存
                showView: true
              });
            };
            if (that.data.goods_storage == 0) {
              this.setData({
                showSure: false
              });
            } else {
              that.setData({
                showSure: true
              })
            }; 
          };
          if (res.data.code == 400) {
            if (res.data.login == 0 && res.data.login) {
              wx.navigateTo({
                url: '/pages/authorize/index',
              })
            } else {
              wx.showToast({
                title: res.data.datas.error,
                icon: 'none',
                duration: 1500,
                mask: true
              })
            };
          }
        },
        fail: () => {
          wx.showToast({
            title: '人太多啦',
            icon: 'none',
            duration: 2000
          })
        }
      });
    }
	},
	/**
   * 加入购物车--提交购买
   * @author chelfinn
   * @date 2018-09-19
   */
	addInCar: function (e) {
		var that = this;
    var name = this.data.propertyChildNames.split(" ");
    if (this.data.specALLArr.length == 0) {
      if (this.data.goods_storage == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他商品~',
        });
        return false;
      };
    } else {
      if (this.data.goods_storage == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他商品~',
          showCancel: false
        });
        return false;
      };
      if (name.length != this.data.specALLArr.length) {
        wx.showModal({
          title: '提示',
          content: '请先选择商品规格和尺寸~',
          showCancel: false,
        });
        return false;
      }
    }
		wx.request({
			url: url + '/mobile/index.php?act=member_cart&op=cart_add',
			header: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			dataType: 'json',
			method: 'POST',
			data: {
				key: wx.getStorageSync('key'),
				goods_id: e.currentTarget.dataset.goods_id,
				quantity: e.currentTarget.dataset.quantity
			},
			success: (res) => {
				if (res.data.code == 200) {
					wx.showToast({
						title: '加入购物车成功',
						icon: 'success',
						duration: 2000
					});
					this.setData({
						showView: false,
						propertyChildNames: '',
					});
				} else {
          if (res.data.login == 0 && res.data.login) {
            wx.navigateTo({
              url: '/pages/authorize/index',
            })
          } else {
            wx.showToast({
              title: res.data.datas.error,
              icon: 'none',
              duration: 1500,
              mask: true
            })
          };
					this.setData({
						showView: false,
						propertyChildNames: '',
					});
				}
			},
      fail: () => {
        wx.showToast({
          title: '人太多啦',
          icon: 'none',
          duration: 2000
        })
      }
		});
	},
	/**
   * 立即购买--跳转到订单页
   * @author chelfinn
   * @date 208-09-20
   */
	buyNow: function (e) {
    /**
     * log_id 参团id,自己发起的团,支付成功后返回的id，有则传，无则不传，不能穿空    pintuan_id 不为空时，为拼主的拼团号
     * pintuan    拼团的话设置为1，默认为空
     * status 1普通购买，2拼团，3秒杀
     */
		var that = this;
		var ifcart = '';
		var cart_id = e.currentTarget.dataset.goods_id + '|' + that.data.num;
		//item 店铺名，商品名，商品图片，选中规格，商品单价，数量
    var item = {};
		item.store_name = that.data.detailData.store_info.store_name;
    item.store_id = that.data.detailData.store_info.store_id;
		item.goods_name = that.data.detailData.goods_info.goods_name;
		item.goods_price = that.data.goods_price;
		item.spec_name = that.data.propertyChildNames;
		item.goods_num = that.data.num;
    item.goods_freight = that.data.detailData.goods_info.goods_freight;
		item = JSON.stringify(item);
		var totalPrice = that.data.goods_price * that.data.num;

    var item1 = JSON.parse(item);
    var storega = that.data.goods_storage;
    var arr = item1.spec_name.split(" ");
    if (this.data.specALLArr.length == 0) {
      if (storega == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他上商品',
          showCancel: false,
        })
        this.setData({
          showView: false
        })
        return;
      }
      wx.navigateTo({
        url: '/pages/submit-order/index?status=1&cart_id=' + cart_id + '&ifcart=' + ifcart + '&item=' + item + '&totalPrice=' + totalPrice,
      });
    } else {
      if (storega == 0) {
        wx.showModal({
          title: '提示',
          content: '库存不足，请购买其他上商品',
          showCancel: false,
        })
        this.setData({
          showView: false
        })
        return;
      }
      if (arr.length != this.data.specALLArr.length) {
        wx.showModal({
          title: '提示',
          content: '请先选择商品规格尺寸哟~',
          showCancel: false,
        })
        return;
      } 
      wx.navigateTo({
        url: '/pages/submit-order/index?status=1&cart_id=' + cart_id + '&ifcart=' + ifcart + '&item=' + item + '&totalPrice=' + totalPrice,
      });
    }
    
	}, 
  //点击减号
  minuAcvive: function() {
    var num = this.data.num;
    if (num > 1) {    //如果num大于1才能减
      num--;
    };
    // 只有大于一件的时候，才能normal状态，否则disable状态
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回
    this.setData({
      num: num,
    });
  },

  // 点击加号 
  addActive: function() {
    var num = this.data.num;
    num++;
    // 将数值与状态写回
    this.setData({
      num: num,
    });
  },
  /**
   * 输入框事件
   */
	bindManual: function (e) {
    var num = e.detail.value;
    // 将数值与状态写回
    this.setData({
      num: num
    });
  },
  alertview: function (e) {
    var currentStatu = e.detail.status;
    switch (currentStatu) {
      case 'open':
        this.setData({
          showModalStatus: true
        })
        break;
      case 'close':
        this.setData({
          showModalStatus: false
        })
        break;
    }
  },
  close: function (e) {
    this.setData({
      showModalStatus: !this.data.showModalStatus
    });
  },

  // 新人红包弹窗
  closeLuckMoney: function() {
    this.setData({
      showLuckMoney: false
    });
  },
  // 限时特惠
  changeOne: function (res) {
    this.setData({
      current_limit: res.currentTarget.dataset.index,
    });
  },
  // 热卖商品
  change: function(res) {
   this.setData({
     current: res.currentTarget.dataset.index,
   })
  },
  // 关闭推荐拿好礼
  toClose: function () {
    this.setData({
      showModalStatus: false
    })
  }, 
})
